package gof.starbuzz.example;

import java.io.*;

/**
 * TODO
 * 2020-11-04 18:02
 */
public class LowerCaseInputStream extends FilterInputStream {

    /**
     * Creates a <code>FilterInputStream</code>
     * by assigning the  argument <code>in</code>
     * to the field <code>this.in</code> so as
     * to remember it for later use.
     *
     * @param in the underlying input stream, or <code>null</code> if
     *           this instance is to be created without an underlying stream.
     */
    protected LowerCaseInputStream(InputStream in) {
        super(in);
    }

    public int read() throws IOException{
        int c = super.read();
        return (c == -1 ? c : Character.toLowerCase((char)c));
    }

    public int read(byte[] n, int offset, int len) throws IOException{
        int result = super.read(n, offset, len);
        for (int i = offset; i < offset + result; i++) {
            n[i] = (byte)Character.toLowerCase((char)n[i]);
        }
        return  result;
    }

    public static void main(String[] args) {
        int c;
        try{
            InputStream lowerCaseInputStream = new LowerCaseInputStream(
                    new BufferedInputStream(
                            new FileInputStream("src/gof/starbuzz/example/test.txt")
                    )
            );

            while ((c = lowerCaseInputStream.read()) >= 0){
                System.out.println((char) c);
            }
            lowerCaseInputStream.close();
        }catch (Exception e){

        }
    }
}
